<?php
namespace jy_aligroup;
defined('BY_JYA') or exit('error');
/*
	order
*/
class order extends \plugin{
	/*
		拼团失败
	*/
	public function order_cancel($teamid){
		$params=array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$teamid,'status'=>1);
		$basic=$this->plugin_setting('basic');
		$order=pdo_getall('jy_aligroup_groups_order',$params,array('id','mid','teamid','goodsid','price','pay_type','pay_id'));
		foreach($order as $item){
			if(!$item['pay_type']){
				$item['pay_type']=pdo_getcolumn('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pay_id']),'pay_type');
			}
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'orderid'=>$item['id'],
				'mid'=>$item['mid'],
				'teamid'=>$teamid,
				'goodsid'=>$item['goodsid'],
				'money'=>$item['price'],
				'type'=>$item['pay_type'],
				'status'=>0,
				'createtime'=>time(),
			);
			pdo_insert('jy_aligroup_groups_cashback', $insert);	
			$refundid = pdo_insertid(); 
			pdo_update('jy_aligroup_groups_order',array('status'=>5,'refundid'=>$refundid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
			if($basic['istui']){
				$this->refund_out($refundid);
			}
		}
		pdo_update('jy_aligroup_groups_team',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$teamid));
	}
	//发货
	public function order_send($order_id,$params=array()){
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'orderid'=>$order_id,
			'express_id'=>intval($params['express']),
			'expresssn'=>$params['expresssn'],
			'opensend_type'=>$params['opensend_type'],
			'createtime'=>TIMESTAMP,
		);
		$this->insert('order_express',$data);
		if($params['send_status']){
			$this->update('groups_order',array('send_status'=>$params['send_status']),array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$this->update('groups_order',array('status'=>3,'sendtime'=>time()),array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
			//$this->get_order($order_id);
			//$this->order['sendtime']=TIMESTAMP;
			//$this->model('notice')->notice_send2buyer($this->order);
		}
	}
	//确认收货，完成订单
	public function order_receive($order_id,$ismsg=false){
		$update=array('status'=>4,'finishtime'=>TIMESTAMP);
		$this->update('groups_order',$update,array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		//$this->get_order($order_id);
		//$this->model('notice')->notice_receive2buyer($this->order);
		return true;
	}
	public function order_send_virtual($order_id,$content=''){
		$this->update('groups_order',array('status'=>3,'sendcontent'=>$content,'sendtime'=>time()),array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function order_verify($verify_code,$clerk_id=0){
		$order_verify=$this->pdo_get('groups_order',array('verifycode'=>$verify_code));
		if(!$order_verify){
			exi('核销码不存在！','error');
		}
		if($order_verify['status']==array(3,4)){
			exi('核销码已被使用！','error');
		}
		if($order_verify['verifytype']==0 || $order_verify['verifytype']==2){
			//按订单核销
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'orderid'=>$order_verify['id'],
				'storeid'=>$order_verify['storeid'],
				'mid'=>$order_verify['mid'],
				'verifier'=>$clerk_id,
				'isverify'=>1,
				'verifycode'=>$order_verify['verifycode'],
				'verifytime'=>TIMESTAMP,
			);
			$this->pdo_insert('groups_verify',$insert);
			$this->order_receive($order_verify['id']);
		}elseif($order_verify['verifytype']==1){
			//按次核销
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'orderid'=>$order_verify['id'],
				'storeid'=>$order_verify['storeid'],
				'mid'=>$order_verify['mid'],
				'verifier'=>$clerk_id,
				'isverify'=>1,
				'verifycode'=>$order_verify['verifycode'],
				'verifytime'=>TIMESTAMP,
			);
			$this->pdo_insert('groups_verify',$insert);
			$verifynum=$this->pdo_count('groups_verify',array('orderid'=>$order_verify['id']));
			if($verifynum>=$order_verify['verifynum']){
				$this->order_receive($order_verify['id']);
			}
		}
	}
	public function check_level(){
		$setting=$this->plugin_setting('basic');
		if(!$setting['showlevels']){
			return true;	
		}
		if($_SESSION['uid']){
			
			$levelid = pdo_getcolumn('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'merchid'=>0),'level');
			if(strpos($setting['showlevels'],';'.$levelid.';') === false){
				exi('只有授权会员才能参与拼团！','error');
			}
		}else{
			exi('只有授权会员才能参与拼团！','error');
		}
		return true;
	}
	public function refund_out($refundid=0){
		$post=pdo_get('jy_aligroup_groups_cashback',array('uniacid'=>$_SESSION['uniacid'],'id'=>$refundid));
		$order=pdo_get('jy_aligroup_groups_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['orderid']));
		$payinfo=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pay_id']));
		if(!$transaction_id){
			$transaction_id=$order['transid']?:$payinfo['transaction_id'];
		}
		$payinfo['money']=$post['money'];
		$payinfo['remark']='拼团退款';
		$payinfo['transid']=$transaction_id;
		$type=$post['type']?:$payinfo['pay_type'];
		if($type=='wechat'){
			$pay_out_id=model('core/member')->refund_out($order['mid'],$payinfo);
		}elseif($type=='credit'){
			model('core/member')->add_credit2(intval($order['mid']),$post['money'],'拼团退款','jy_aligroup');
		}elseif($type=='alipay'){
			$pay_out_id=model('core/member')->refund_out($order['mid'],$payinfo,'','alipay');
		}else{
			model('core/member')->add_credit2(intval($order['mid']),$post['money'],'拼团退款','jy_aligroup');
		}
		pdo_update('jy_aligroup_groups_cashback',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$refundid));
		pdo_update('jy_aligroup_groups_order',array('status'=>6,'refundtime'=>time()),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']));
		return true;
	}
}